/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.component.controls;

import cn.ac.nercis.pes.community.support.event.EventCallback;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.shape.Rectangle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.util.Objects;

/**
 * 模块菜单控件
 * @author zpy
 * @version 1.0.0
 */
@Slf4j
public class MenuBlockControl extends AnchorPane {
    private final static Double WIDTH = 250.0;
    private final static Double HEIGHT = 150.0;

    private final ImageView backgroundImgView;
    private final Button button;
    private final Label menuLbl;
    private EventCallback<String> onClick;

    public MenuBlockControl(String bgImageUrl,String menuText,EventCallback<String> onClick){
        this();
        this.initControl(bgImageUrl,menuText);
        this.onClick = onClick;
    }


    public MenuBlockControl() {
        this.getStyleClass().add("menu-block");
        this.backgroundImgView = new ImageView();
        this.backgroundImgView.setFitWidth(WIDTH);
        this.backgroundImgView.setFitHeight(HEIGHT);
        this.button = new Button();
        this.button.getStyleClass().add("menu-block-button");
        this.menuLbl = new Label();
        this.menuLbl.getStyleClass().add("menu-block-name");
        AnchorPane.setBottomAnchor(this.menuLbl,20.0);
        AnchorPane.setLeftAnchor(this.menuLbl,20.0);
        this.getChildren().addAll(backgroundImgView,button,menuLbl);
        this.onClick = null;
    }

    public void initControl(String bgImageUrl,String menuText){
        try{
            var resource= new ClassPathResource(bgImageUrl);
            var image = new Image(resource.getInputStream(),WIDTH,HEIGHT,false,false);
            backgroundImgView.setImage(image);
            var clip = new Rectangle(backgroundImgView.getFitWidth(),backgroundImgView.getFitHeight());
            clip.setArcWidth(25);
            clip.setArcHeight(25);
            backgroundImgView.setClip(clip);
            this.menuLbl.setText(menuText);
            this.button.setOnAction((event)->{
                if(Objects.nonNull(onClick)){
                    this.onClick.action(this.menuLbl.getText());
                }
            });
        }catch (IOException ex){
            log.error("加载背景图像失败：",ex);
        }
    }

    public void setOnClick(EventCallback<String> onClick){
        this.onClick = onClick;
    }
}